﻿// Decompiled with JetBrains decompiler
// Type: System.Web.Razor.Text.TextBufferReader
// Assembly: System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// MVID: 682513D6-F37E-48AC-9909-F3EE103CFF14
// Assembly location: D:\Project\Idea\无人机外网\uav\客户端工程\排查工具\bin\Release\System.Web.Razor.dll

using System.Collections.Generic;
using System.Web.Razor.Resources;
using System.Web.Razor.Utils;

namespace System.Web.Razor.Text
{
  /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Provides a reader for text buffer.</summary>
  public class TextBufferReader : LookaheadTextReader
  {
    private Stack<TextBufferReader.BacktrackContext> _bookmarks = new Stack<TextBufferReader.BacktrackContext>();
    private SourceLocationTracker _tracker = new SourceLocationTracker();

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Initializes a new instance of the <see cref="T:System.Web.Razor.Text.TextBufferReader" /> class.</summary>
    /// <param name="buffer">The text buffer to read.</param>
    public TextBufferReader(ITextBuffer buffer) => this.InnerBuffer = buffer != null ? buffer : throw new ArgumentNullException(nameof (buffer));

    internal ITextBuffer InnerBuffer { get; private set; }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Gets the current location of the text buffer.</summary>
    /// <returns>The current location of the text buffer.</returns>
    public override SourceLocation CurrentLocation => this._tracker.CurrentLocation;

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Returns the next text buffer to read.</summary>
    /// <returns>The next text buffer to read.</returns>
    public override int Peek() => this.InnerBuffer.Peek();

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Reads the current text buffer.</summary>
    /// <returns>The current text buffer.</returns>
    public override int Read()
    {
      int num1 = this.InnerBuffer.Read();
      if (num1 != -1)
      {
        char nextCharacter = char.MinValue;
        int num2 = this.Peek();
        if (num2 != -1)
          nextCharacter = (char) num2;
        this._tracker.UpdateLocation((char) num1, nextCharacter);
      }
      return num1;
    }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Releases the unmanaged resources used by the <see cref="T:System.Web.Razor.Text.TextBufferReader" /> class and optionally releases the managed resources.</summary>
    /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
    protected override void Dispose(bool disposing)
    {
      if (disposing && this.InnerBuffer is IDisposable innerBuffer)
        innerBuffer.Dispose();
      base.Dispose(disposing);
    }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Begins reading the current text buffer.</summary>
    /// <returns>An <see cref="T:System.IDisposable" /> instance that stops the text buffer.</returns>
    public override IDisposable BeginLookahead()
    {
      TextBufferReader.BacktrackContext context = new TextBufferReader.BacktrackContext()
      {
        Location = this.CurrentLocation
      };
      this._bookmarks.Push(context);
      return (IDisposable) new DisposableAction((Action) (() => this.EndLookahead(context)));
    }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Cancels backtrack.</summary>
    public override void CancelBacktrack()
    {
      if (this._bookmarks.Count == 0)
        throw new InvalidOperationException(RazorResources.CancelBacktrack_Must_Be_Called_Within_Lookahead);
      this._bookmarks.Pop();
    }

    private void EndLookahead(TextBufferReader.BacktrackContext context)
    {
      if (this._bookmarks.Count <= 0 || this._bookmarks.Peek() != context)
        return;
      this._bookmarks.Pop();
      this._tracker.CurrentLocation = context.Location;
      this.InnerBuffer.Position = context.Location.AbsoluteIndex;
    }

    private class BacktrackContext
    {
      public SourceLocation Location { get; set; }
    }
  }
}
